home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / lsof_3.37 / scripts / list_fields.perl < prev    next >
Encoding:
Text File  |  1995-07-31  |  4.1 KB  |  145 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # $Id: list_fields.perl,v 1.7 95/06/30 08:56:03 abe Exp $
  4. #
  5. # list_fields.perl -- sample Perl script to list lsof 3.33 and above full
  6. #              field output (i.e., -F output without -0)
  7. #
  8. # This script has been tested under perl versions 4.036 and 5.001e.
  9. #
  10. # Copyright 1994 Purdue Research Foundation, West Lafayette, Indiana
  11. # 47907.  All rights reserved.
  12. #
  13. # Written by Victor A. Abell
  14. #
  15. # This software is not subject to any license of the American Telephone
  16. # and Telegraph Company or the Regents of the University of California.
  17. #
  18. # Permission is granted to anyone to use this software for any purpose on
  19. # any computer system, and to alter it and redistribute it freely, subject
  20. # to the following restrictions:
  21. #
  22. # 1. Neither the authors nor Purdue University are responsible for any
  23. #    consequences of the use of this software.
  24. #
  25. # 2. The origin of this software must not be misrepresented, either by
  26. #    explicit claim or by omission.  Credit to the authors and Purdue
  27. #    University must appear in documentation and sources.
  28. #
  29. # 3. Altered versions must be plainly marked as such, and must not be
  30. #    misrepresented as being the original software.
  31. #
  32. # 4. This notice may not be removed or altered.
  33.  
  34. # Initialize variables.
  35.  
  36. $fhdr = 0;                            # fd hdr. flag
  37. $fdst = 0;                            # fd state
  38. $access = $devch = $devn = $fd = $inode = $lock = "";        # | file descr.
  39. $name = $offset = $proto = $size = $stream = $type = "";    # | variables
  40. $pidst = 0;                            # process state
  41. $cmd = $login = $pgrp = $pid = $uid = "";            # process var.
  42.  
  43. # Process the ``lsof -F'' output a line at a time, gathering
  44. # the variables for a process together before printing them;
  45. # then gathering the variables for each file descriptor
  46. # together before printing them.
  47.  
  48. while (<>) {
  49.     chop;
  50.     if (/^p(.*)/) {
  51.  
  52. # A process set begins with a PID field whose ID character is `p'.
  53.  
  54.     $tpid = $1;
  55.     if ($pidst) { &list_proc }
  56.     $pidst = 1;
  57.     $pid = $tpid;
  58.     if ($fdst) { &list_fd; $fdst = 0; }
  59.     next;
  60.     }
  61.  
  62. # Save process-related values.
  63.  
  64.     if (/^g(.*)/) { $pgrp = $1; next; }
  65.     if (/^c(.*)/) { $cmd = $1; next; }
  66.     if (/^u(.*)/) { $uid = $1; next; }
  67.     if (/^L(.*)/) { $login = $1; next; }
  68.  
  69. # A file descriptor set begins with a file descriptor field whose ID
  70. # character is `f'.
  71.  
  72.     if (/^f(.*)/) {
  73.     $tfd = $1;
  74.     if ($pidst) { &list_proc }
  75.     if ($fdst) { &list_fd }
  76.     $fd = $tfd;
  77.     $fdst = 1;
  78.     next;
  79.     }
  80.  
  81. # Save file set information.
  82.  
  83.     if (/^a(.*)/) { $access = $1; next; }
  84.     if (/^l(.*)/) { $lock = $1; next; }
  85.     if (/^t(.*)/) { $type = $1; next; }
  86.     if (/^d(.*)/) { $devch = $1; next; }
  87.     if (/^D(.*)/) { $devn = $1; next; }
  88.     if (/^s(.*)/) { $size = $1; next; }
  89.     if (/^o(.*)/) { $offset = $1; next; }
  90.     if (/^i(.*)/) { $inode = $1; next; }
  91.     if (/^P(.*)/) { $proto = $1; next; }
  92.     if (/^S(.*)/) { $stream = $1; next; }
  93.     if (/^n(.*)/) { $name = $1; next; }
  94.     print "ERROR: unrecognized: \"$_\"\n";
  95. }
  96.  
  97. # Flush any stored file or process output.
  98.  
  99. if ($fdst) { &list_fd }
  100. if ($pidst) { &list_proc }
  101. exit(0);
  102.  
  103.  
  104. ## list_fd -- list file descriptor information
  105. #          Values are stored inelegantly in global variables.
  106.  
  107. sub list_fd {
  108.     if ( ! $fhdr) {
  109.  
  110.     # Print header once.
  111.  
  112.     print "      FD   TYPE      DEVICE   SIZE/OFF      INODE  NAME\n";
  113.     $fhdr = 1;
  114.     }
  115.     printf "    %4s%1.1s%1.1s %4.4s", $fd, $access, $lock, $type;
  116.     $tmp = $devn; if ($devch ne "") { $tmp = $devch }
  117.     printf "  %10.10s", $tmp;
  118.     $tmp = $size; if ($offset ne "") { $tmp = $offset }
  119.     printf " %10.10s", $tmp;
  120.     $tmp = $inode; if ($proto ne "") { $tmp = $proto }
  121.     printf " %10.10s", $tmp;
  122.     $tmp = $stream; if ($name ne "") { $tmp = $name }
  123.     print "  ", $tmp, "\n";
  124.  
  125. # Clear variables.
  126.  
  127.     $access = $devch = $devn = $fd = $inode = $lock = "";
  128.     $name = $offset = $proto = $size = $stream = $type = "";
  129. }
  130.  
  131.  
  132. # list_proc -- list process information
  133. #           Values are stored inelegantly in global variables.
  134.  
  135. sub list_proc {
  136.     print "COMMAND       PID    PGRP  USER\n";
  137.     $tmp = $uid; if ($login ne "") {$tmp = $login }
  138.     printf "%-9.9s  %6d  %6d  %s\n", $cmd, $pid, $pgrp, $tmp;
  139.  
  140. # Clear variables.
  141.  
  142.     $cmd = $login = $pgrp = $pid = $uid = "";
  143.     $fhdr = $pidst = 0;
  144. }
  145.